package iscript.uiFramework.components.panel
{
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.events.MouseEvent;
	import iscript.uiFramework.components.container.Box;
	import iscript.uiFramework.components.container.Container;
	import iscript.uiFramework.components.container.IBox;
	import iscript.uiFramework.components.container.IContainer;
	import iscript.uiFramework.components.layout.ILayoutManager;
	import iscript.uiFramework.components.silder.SilderBar;
	import iscript.uiFramework.components.silder.SilderPolicy;
	import iscript.uiFramework.components.silder.SilderType;
	import iscript.uiFramework.components.UIComponent;
	import iscript.uiFramework.event.ScrollPanelEvent;
	import iscript.uiFramework.event.SilderEvent;
	
	[Event(name="draging",type="iscript.uiFramework.event.ScrollPanelEvent")]
	
	public class ScrollPanel extends UIComponent 
	{
		private var _wheelBet:Number = 1.5;
		private var _VSilder:SilderBar;
		private var _HSilder:SilderBar;
		private var _VSilderPolicy:String;
		private var _HSilderPolicy:String;
		private var _width:Number;
		private var _height:Number;
		private var _body:ScrollPanelBody;
		private var _scrollDrag:Boolean; 
		
		public function ScrollPanel()
		{
			_VSilderPolicy = SilderPolicy.AUTO;
			_HSilderPolicy = SilderPolicy.AUTO;	
			super();
		}
		
		override protected function initialize():void
		{
			_body = new ScrollPanelBody();
			_body.render(this)
			_VSilder = new SilderBar();
			_VSilder.type = SilderType.Vertical;
			_HSilder = new SilderBar();
			_VSilder.render(this);
			_HSilder.render(this);
			this.width = 100;
			this.height = 100;
			super.initialize();
		}
		
		override protected function eventListener():void 
		{
			this.addEventListener(MouseEvent.MOUSE_WHEEL, _mouseWheelLogic);
			_body.addEventListener(ScrollPanelEvent.DRAGING, _panelDragLogic);
			_VSilder.addEventListener(SilderEvent.DRAGING, _VSilderDragLogic);
			_HSilder.addEventListener(SilderEvent.DRAGING, _HSilderDragLogic);
			super.eventListener();
		}
		
		
		private function _VSilderDragLogic(e:SilderEvent):void
		{
			_body.processHeight = e.process;
		}
		
		private function _HSilderDragLogic(e:SilderEvent):void
		{
			_body.processWidth = e.process;
		}
		
		public function sourceUpdate():void
		{
			SilderPolicyLogic();
			_autoBoxSize();
			updateRender();
		}
		
		override public function render(container:DisplayObjectContainer = null):void
		{
			if(source!=null)
			sourceUpdate();
			background = true;
			getBackground().begFillColor(0xFFFFFF)
			super.render(container);
		}
		
		override public function dispose():void
		{
			if (!Disposed)
			{
				_VSilder.removeEventListener(SilderEvent.DRAGING, _VSilderDragLogic);
				_HSilder.removeEventListener(SilderEvent.DRAGING, _HSilderDragLogic);
				addEventListener(ScrollPanelEvent.DRAGING, _panelDragLogic);
				addEventListener(MouseEvent.MOUSE_WHEEL, _mouseWheelLogic);
				_body.dispose()
				super.dispose();
			} 
		}
		
		/**
		 * Silder Visible 逻辑
		 */
		protected function SilderPolicyLogic():void
		{
			
			if (_VSilderPolicy == SilderPolicy.OFF)
			{
				_VSilder.visible = false;
			}
			if (_VSilderPolicy == SilderPolicy.ON)
			{
				_VSilder.visible = true;
			}
			
			if (_VSilderPolicy == SilderPolicy.AUTO)
			{
				if(_HSilder.visible)
					_VSilder.visible = this._body.container.height > this.height - _HSilder.height ;
				else
					_VSilder.visible = this._body.container.height > this.height;
				
			}
			if (_HSilderPolicy == SilderPolicy.OFF)
			{
				_HSilder.visible = false;
			}
			if (_HSilderPolicy == SilderPolicy.ON)
			{
				_HSilder.visible = true;
			}
			if (_HSilderPolicy == SilderPolicy.AUTO)
			{
				if(_VSilder.visible)
					_HSilder.visible = this._body.container.width > this.width- _VSilder.width;
				else
					_HSilder.visible = this._body.container.width > this.width
			}
		
		}
		
		private function _autoBoxSize():void
		{
			_VSilder.width = this.height - _VSilder.width;
			_VSilder.x = this.width - _VSilder.width;
			
			_HSilder.width = this.width - _HSilder.height;
			_HSilder.y = this.height - _HSilder.height;
			
		 
			_HSilder.progress = _body.processWidth
			_VSilder.progress = _body.processHeight
			
			
			if (_VSilder.visible && _HSilder.visible)
			{
				_body.width = this.width - _VSilder.width
				_body.height = this.height - _HSilder.height
			}
			
			if (!_VSilder.visible &&!_HSilder.visible)
			{
				_body.width = this.width;
				_body.height = this.height;
			}
			
			if (_VSilder.visible && !_HSilder.visible)
			{
				_body.width = this.width - _VSilder.width;
				_body.height = this.height;
				_VSilder.width = this.height;
			}
			if (!_VSilder.visible && _HSilder.visible)
			{
				_body.width = this.width;
				_body.height = this.height - _HSilder.height;
				_HSilder.width = this.width;
			}

			 _body.processWidth = _HSilder.progress 
			 _body.processHeight = _VSilder.progress
		}
		
		public function get VSilderPolicy():String
		{
			return _VSilderPolicy;
		}
		
		public function set VSilderPolicy(value:String):void
		{
			_VSilderPolicy = value;
			if (IsRender)
				sourceUpdate();
		
		}
		
		public function get HSilderPolicy():String
		{
			return _HSilderPolicy;
		}
		
		public function set HSilderPolicy(value:String):void
		{
			_HSilderPolicy = value;
			if (IsRender)
				sourceUpdate();
		}
		
		override public function get width():Number
		{
			return _width;
		}
		
		override public function set width(value:Number):void
		{
			_width = value;
			_body.width = value;
			if (IsRender)
				sourceUpdate();
		}
		
		override public function get height():Number
		{
			return _height;
		}
		
		override public function set height(value:Number):void
		{
			_height = value;
			_body.height = value;
			if (IsRender)
				sourceUpdate();
		}
		
		private function _panelDragLogic(e:ScrollPanelEvent):void
		{
			_VSilder.progress = e.processHeight;
			_HSilder.progress = e.processWidth;
		}
		
		public function get source():DisplayObject
		{
			return _body.source;
		}
		
		public function set source(value:DisplayObject):void
		{
			_body.source = value;
			if (IsRender)
				sourceUpdate();
		}
		
		public function set processWidth(value:Number):void
		{
			_HSilder.progress = value;
			if(IsRender)
			_body.processWidth = value;
		}
		
		public function get processWidth():Number
		{
			return _body.processWidth
		}
		public function set processHeight(value:Number):void
		{
			_VSilder.progress = value;
			if(IsRender)
			_body.processHeight = value;
		}
		
		public function get processHeight():Number
		{
			return _body.processHeight
		}
		
		public function get scrollDrag():Boolean
		{
			return _scrollDrag;
		}
		
		public function set scrollDrag(value:Boolean):void
		{
			_scrollDrag = value;
			_body.scrollDrag = value;
		}
				
 
		 
		/**
		 * 滚轮事件
		 * @param	e
		 */
		private function _mouseWheelLogic(e:MouseEvent):void
		{
			if (_VSilder.visible)
			{
				_VSilder.currentValue += e.delta * -1 * _wheelBet;
				this.processHeight = _VSilder.progress;
			}
			
			if (_HSilder.visible && !_VSilder.visible)
			{
				_HSilder.currentValue += e.delta * -1 * _wheelBet;
				this.processWidth = _HSilder.progress;
			}
		}
	}

}